<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="付款账户" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入付款账户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="充值账户" prop="activateUserId">
        <el-input
          v-model="queryParams.activateUserId"
          placeholder="请输入充值账户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus">
          <el-option
            v-for="item in this.orderStatus"
            :key="item.order_status"
            :label="item.status_name"
            :value="item.order_status">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否删除" prop="orderDelete">
        <el-select v-model="queryParams.orderDelete">
          <el-option
            key="0"
            label="未删除"
            value="0">
          </el-option>
          <el-option
            key="1"
            label="已删除"
            value="1">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-if="1==0"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['order:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-if="1==0"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['order:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-if="1==0"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['order:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="orderId" />
      <el-table-column label="付款编号" align="center" prop="userId" />
      <el-table-column label="付款账户" align="center" prop="userAccount" />
      <el-table-column label="付款昵称" align="center" prop="userName" />
      <el-table-column label="充值编号" align="center" prop="activateUserId" />
      <el-table-column label="充值账户" align="center" prop="activateUserAccount" />
      <el-table-column label="充值昵称" align="center" prop="activateUserName" />
      <el-table-column label="订单状态" align="center" prop="orderStatus" :formatter="convertStatus"/>
      <el-table-column label="数量" align="center" prop="orderCount" />
      <el-table-column label="是否删除" align="center" prop="orderDelete">
        <template slot-scope="scope">
          <span :class="scope.row.orderDelete?'red' : 'green'">
            {{ scope.row.orderDelete? '已删除' : '未删除' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="总价" align="center" prop="orderTotal" />
      <el-table-column label="创建时间" align="center" prop="orderCreateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderCreateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['order:order:edit']"
            v-if="1==0"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['order:order:remove']"
            v-if="1==0"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDetail(scope.row)"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div>
      <!--  音乐订单详情对话框   -->
      <el-dialog :title="title" :visible.sync="orderMusicFormVisible">
        <el-descriptions :column="4">
          <el-descriptions-item label="订单编号" :span="4">{{order.orderId}}</el-descriptions-item>
          <el-descriptions-item label="订单创建时间" >{{order.orderCreateTime}}</el-descriptions-item>
          <el-descriptions-item label="购买数量" >{{order.orderCount}}</el-descriptions-item>
          <el-descriptions-item label="订单总价" >{{order.orderTotal}}</el-descriptions-item>
          <el-descriptions-item label="订单状态" >{{order.orderStatus}}</el-descriptions-item>

          <el-descriptions-item label="付款者编号" :span="2">{{order.userId}}</el-descriptions-item>
          <el-descriptions-item label="付款账号">{{order.userAccount}}</el-descriptions-item>
          <el-descriptions-item label="付款昵称">{{order.userName}}</el-descriptions-item>
          <el-descriptions-item label="购买者编号" :span="2">{{order.activateUserId}}</el-descriptions-item>
          <el-descriptions-item label="购买账号">{{order.activateUserAccount}}</el-descriptions-item>
          <el-descriptions-item label="购买昵称">{{order.activateUserName}}</el-descriptions-item>
        </el-descriptions>
        <el-form>
          <el-form-item>
            <el-table ref="multipleTable" :data="musicList" tooltip-effect="dark" style="width: 100%;">
              <el-table-column prop="musicId" label="音乐编号" width="170" align="center"></el-table-column>
              <el-table-column prop="musicName" label="音乐名称" width="120" align="center"></el-table-column>
              <el-table-column prop="singerName" label="歌手姓名" width="320" align="center"></el-table-column>
            </el-table>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="orderMusicFormVisible=false">取 消</el-button>
        </div>
      </el-dialog>
    </div>

    <!-- 添加或修改订单列表对话框 -->
<!--    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="付款账户" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入付款账户" />
        </el-form-item>
        <el-form-item label="充值账户" prop="activateUserId">
          <el-input v-model="form.activateUserId" placeholder="请输入充值账户" />
        </el-form-item>
        <el-form-item label="数量" prop="orderCount">
          <el-input v-model="form.orderCount" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="总价" prop="orderTotal">
          <el-input v-model="form.orderTotal" placeholder="请输入总价" />
        </el-form-item>
        <el-form-item label="创建时间" prop="orderCreateTime">
          <el-date-picker clearable
            v-model="form.orderCreateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>-->
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/order/order";
import orderStatus from "@/utils/orderStatus";
import request from "@/utils/request";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单列表表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        activateUserId: null,
        orderStatus: null,
        orderCount: null,
        orderDelete: null,
        orderTotal: null,
        orderCreateTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      orderStatus: orderStatus,

      orderMusicFormVisible:false,
      order:{},
      musicList:[]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单列表列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        userId: null,
        activateUserId: null,
        orderStatus: null,
        orderCount: null,
        orderDelete: null,
        orderTotal: null,
        orderCreateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrder(orderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$modal.confirm('是否确认删除订单列表编号为"' + orderIds + '"的数据项？').then(function() {
        return delOrder(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /**查看订单详情*/
    handleDetail(row){
      this.title = '查看订单信息'
      this.order.orderId = row.orderId
      this.order.userId = row.userId
      this.order.userName = row.userName
      this.order.userAccount = row.userAccount
      this.order.activateUserId = row.activateUserId
      this.order.activateUserName = row.activateUserName
      this.order.activateUserAccount = row.activateUserAccount
      this.order.orderStatus = row.orderStatus
      this.getStatus(row.orderStatus)
      this.order.orderCount = row.orderCount
      this.order.orderDelete = row.orderDelete
      this.order.orderTotal = row.orderTotal
      this.order.orderCreateTime = row.orderCreateTime
      this.orderMusicFormVisible = true


      request({
        url: '/order/ordermusic/web/find',
        method: 'get',
        params: {
          orderId: row.orderId
        }, // 传递订单数据
      }).then(response => {
        if (response.code === 200) {
          this.musicList = response.data
        }
      })
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('order/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    },
    /** 订单状态 */
    convertStatus(row, column, cellValue){
      for (let i = 0; i < orderStatus.length; i++) {
        console.log(orderStatus[i].order_status)
        if(cellValue==orderStatus[i].order_status){
          return orderStatus[i].status_name;
        }
      }
    },
    getStatus(status){
      for (let i = 0; i < orderStatus.length; i++) {
        if(orderStatus[i].order_status==status){
          this.order.orderStatus=orderStatus[i].status_name
        }
      }
    }
  }
};
</script>
